package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.CarCharge;
import com.group.Enity.PropertyInfo;
import com.group.Service.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//propertyinfo/queryPropertyAll2?page=1&limit=15
@Controller
@RequestMapping("propertyinfo")
public class PropertyController {


    @Autowired
    private PropertyService propertyService;

    @ResponseBody
    @RequestMapping("queryPropertyAll")
    public String queryPropertyAll(Integer page,Integer limit,Integer status){

        PageHelper.startPage(page,limit);
        List<PropertyInfo> list = propertyService.getPropertys(status);

        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json.toString();
    }

    @ResponseBody
    @RequestMapping("queryPropertyAll2")
    public String queryPropertyAll2(Integer page,Integer limit,Integer status){

        PageHelper.startPage(page,limit);
        List<PropertyInfo> list = propertyService.getPropertys(status);

        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json.toString();
    }

    @ResponseBody
    @RequestMapping("deleteByIds")
    public JSONObject deleteByIds(String ids){

        //System.out.println(ids);
        String[] list = ids.split(",");
        for (int step = 0;step<list.length;step++)
        {
            int id = Integer.parseInt(list[step]);
            propertyService.delByID(id);
        }

        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json;
    }

    @ResponseBody
    @RequestMapping("update")
    public JSONObject update(String id)
    {
        //System.out.println(id);
        propertyService.updateStatus(Integer.parseInt(id));
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json;
    }

    @ResponseBody
    @RequestMapping("initData")
    public JSONObject initData(@RequestBody JSONObject jsonObject)
    {

        //System.out.println(jsonObject);
        PropertyInfo propertyInfo = new PropertyInfo();
        propertyInfo.setEndDate(jsonObject.getStr("endDate"));
        propertyInfo.setStartDate(jsonObject.getStr("startDate"));
        propertyInfo.setRemarks(jsonObject.getStr("remarks"));

        propertyService.update(propertyInfo);
        //propertyService.updateStatus(Integer.parseInt(id));
        JSONObject json=new JSONObject();
        json.accumulate("code",0);
        json.accumulate("msg","wdnmd");
        return json;
    }


}
